﻿using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Linq;
using System.Xml.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using cz.uhul.DSMLv2;

namespace testDsmlSvr
{
  public class testSvrRequest
  {

    public static void go()
    {
      LdRequestDocument r = new LdRequestDocument()
      {
        //AuthRequest = new LdAuthRequest() { Principal = "tst" }
        //,
        DocumentProcessing = LdDocumentProcessing.Parallel
        ,ResponseOrder= LdResponseOrderEnum.Unordered
        ,AddRequests = new LdRequestsCollection()
        {
          new LdAddRequest()
          {
            DistinguishedName="CN=Alice,OU=HR,DC=Example,DC=COM"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "person"
                }
              }
              ,new LdAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "alice"
                }
              }
            }
          }
          ,new LdAddRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "person"
                }
              }
              ,new LdAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob"
                }
              }
            }
          }
        }
        //, DeleteRequests = new LdDeleteRequestCollection()
        //{
        //  new LdDeleteRequest()
        //  {
        //    DistinguishedName="CN=Alice,OU=HR,DC=Example,DC=COM"
        //  }

        //  ,new LdDeleteRequest()
        //  {
        //    DistinguishedName="OU=HR,DC=Example,DC=COM"
        //    ,Controls = new LdControlCollection()
        //    {
        //      new LdControl()
        //      {
        //        Type = "1.2.840.113556.1.4.805"
        //      }
        //    }
        //  }
        //}
        //,
        //ModifyRequests = new LdModifyRequestCollection()
        //{
        //  new LdModifyRequest()
        //  {
        //    DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
        //    ,Attributes = new LdModifyAttrCollection()
        //    {
        //      new LdModifyAttr()
        //      {
        //        Name = "uid"
        //        ,Values = new ObservableCollection<string>()
        //        {
        //          "bob-d"
        //        }
        //        ,Operation = "replace"
        //      }
        //    }
        //  }
        //}



      };

      Console.WriteLine("test request**********************");
      Console.Write(r.ToXml().DocumentElement.OuterXml.ToString());


      ServiceReference1.LdConnectionClient cl = new ServiceReference1.LdConnectionClient();

      XmlNode resp;

      //resp = cl.Process((XmlNode)r.ToXml().DocumentElement);



      string postMsg = @"<?xml version='1.0' encoding='UTF-8'?>
    <batchRequest
      xmlns:xsd='http://www.w3.org/2001/XMLSchema'
      xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
      xmlns='urn:oasis:names:tc:DSML:2:0:core'
      requestID='Batch of search requests'
      responseOrder='Unordered'
    >
      <searchRequest
          dn=''
          requestID='search on Root DSE'
          scope='baseObject'
          derefAliases='neverDerefAliases'
          typesOnly='false'
          >
          <filter>
              <present name='objectClass'/>
          </filter>
          <attributes>
              <attribute name='namingContexts'/>
              <attribute name='supportedLDAPversion'/>
              <attribute name='vendorName'/>
              <attribute name='vendorVersion'/>
              <attribute name='supportedSASLMechanisms'/>
          </attributes>
      </searchRequest>
    </batchRequest>
";

      LdRequestDocument rq = LdRequestDocument.Parse(postMsg);

      XmlDocument x = new XmlDocument();
      x.LoadXml(postMsg);

      XDocument xld = XDocument.Parse(postMsg);

      MessageVersion ver = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None);

      Message msg = Message.CreateMessage(ver, "http://tempuri.org/ILdConnection/Process", xld.CreateReader());

      Message test = cl.Process(msg);

      XmlDocument outDoc = new XmlDocument();
      XPathNavigator nav = outDoc.CreateNavigator();

      XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(nav.AppendChild());

      test.WriteMessage(writer);

      writer.Close();

      //ServiceReference1.LdConnectionClient cl = new ServiceReference1.LdConnectionClient();

      //XmlNode resp = cl.Process((XmlNode)r.ToXml().DocumentElement);

      //LdResponseDocument res = LdResponseDocument.Parse(resp);

      /*ServiceReference1.DsmlBatchResponse rsp =*/
      //LdResponseDocument rd = cl.Process(sr1.batchRequest);


      //cz.uhul.DSML.LdConnectionSoapClient c = new cz.uhul.DSML.LdConnectionSoapClient();

      //XmlNode[] nodes = r.ToXml().ChildNodes.OfType<XmlNode>().ToArray<XmlNode>();

      //c.BatchRequest("", nodes, "ssdf", "", "");

      //uhul.Dsml.LdConnectionSoapClient c = new uhul.Dsml.LdConnectionSoapClient();
      //XmlElement x = c.BatchRequest(
      //  r.ToArray()
      //  , null
      //  , LdResponseOrderEnum.Unordered.ToString()
      //  , r.DocumentProcessing.ToString()
      //  , LdErrorProcessingEnum.Exit.ToString()
      //);


      //testDsmlSvr.uhul.Dsml.LdRequest
      //Console.WriteLine("test response**********************");
      //Console.Write(x.OuterXml.ToString());

    }





  }

}
